/************************************************************
  *  * EaseMob CONFIDENTIAL 
  * __________________ 
  * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved. 
  *  
  * NOTICE: All information contained herein is, and remains 
  * the property of EaseMob Technologies.
  * Dissemination of this information or reproduction of this material 
  * is strictly forbidden unless prior written permission is obtained
  * from EaseMob Technologies.
  */

#import "DXRecordView.h"
#import "EMCDDeviceManager.h"

@interface DXRecordView ()
{
    NSTimer *_timer;
    NSTimer *_timer2;
    // 显示动画的ImageView
    UIImageView *_recordAnimationView;
    // 提示文字
    UILabel *_textLabel;
    UILabel *_timeLabel;
    NSInteger time;
}

@end

@implementation DXRecordView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        UIView *bgView = [[UIView alloc] initWithFrame:self.bounds];
        bgView.backgroundColor = [UIColor whiteColor];
        bgView.clipsToBounds = YES;
        bgView.layer.cornerRadius = 5;
        bgView.layer.masksToBounds = YES;
        [bgView.layer setBorderColor:[RGBA(153, 153, 153, 1) colorWithAlphaComponent:0.5] .CGColor];
        [bgView.layer setBorderWidth:1];
        bgView.alpha = 0.9;
        [self addSubview:bgView];
        
        _recordAnimationView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 25, self.bounds.size.width - 60, self.bounds.size.width - 60)];
        _recordAnimationView.image = [UIImage imageNamed:@"voicesearchfeedback001″"];
        [_recordAnimationView setContentMode:UIViewContentModeScaleAspectFill];
        [self addSubview:_recordAnimationView];
        
        
        _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(5,0,self.bounds.size.width ,25)];
        _timeLabel.textAlignment = NSTextAlignmentCenter;
        _timeLabel.backgroundColor = [UIColor clearColor];
        _timeLabel.text = @"0″";
        [bgView addSubview:_timeLabel];
        _timeLabel.font = [UIFont fontWithName:FontName size:13];
        _timeLabel.textColor = RGBA(153, 153, 153, 1);
        
        
        _textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,self.bounds.size.height-25,self.bounds.size.width ,25)];
        _textLabel.textAlignment = NSTextAlignmentCenter;
        _textLabel.backgroundColor = [UIColor clearColor];
        _textLabel.text = NSLocalizedString(@"message.toolBar.record.upCancel", @"Fingers up slide, cancel sending");
        [bgView addSubview:_textLabel];
        _textLabel.font = [UIFont fontWithName:FontName size:13];
        _textLabel.textColor = RGBA(153, 153, 153, 1);
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, self.bounds.size.height - 25, self.bounds.size.width, 0.25)];
        line.backgroundColor =  RGBA(153, 153, 153, 1);
        [self addSubview:line];
    }
    return self;
}

// 录音按钮按下
-(void)recordButtonTouchDown
{
    time = 0;
    _timeLabel.text = [NSString stringWithFormat:@"%ld″",(long)time];
    // 需要根据声音大小切换recordView动画
    _textLabel.text = NSLocalizedString(@"message.toolBar.record.upCancel", @"Fingers up slide, cancel sending");
    _textLabel.backgroundColor = [UIColor clearColor];
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.05
                                              target:self
                                            selector:@selector(setVoiceImage)
                                            userInfo:nil
                                             repeats:YES];
    
    _timer2 = [NSTimer scheduledTimerWithTimeInterval:1
                                               target:self
                                             selector:@selector(timerLabel)
                                             userInfo:nil
                                              repeats:YES];

    
}
// 手指在录音按钮内部时离开
-(void)recordButtonTouchUpInside
{
    time = 0;
    _timeLabel.text = [NSString stringWithFormat:@"%ld″",(long)time];
    _textLabel.textColor = RGBA(153, 153, 153, 1);
    [_timer invalidate];
    [_timer2 invalidate];
}
// 手指在录音按钮外部时离开
-(void)recordButtonTouchUpOutside
{
    time = 0;
    _timeLabel.text = [NSString stringWithFormat:@"%ld″",(long)time];
    _textLabel.textColor = RGBA(153, 153, 153, 1);
    [_timer invalidate];
    [_timer2 invalidate];
}
// 手指移动到录音按钮内部
-(void)recordButtonDragInside
{
    _textLabel.text = NSLocalizedString(@"message.toolBar.record.upCancel", @"Fingers up slide, cancel sending");
    _textLabel.backgroundColor = [UIColor clearColor];
    _textLabel.textColor = RGBA(153, 153, 153, 1);
}

// 手指移动到录音按钮外部
-(void)recordButtonDragOutside
{
    _textLabel.textColor = [UIColor whiteColor];
    _textLabel.text = NSLocalizedString(@"message.toolBar.record.loosenCancel", @"loosen the fingers, to cancel sending");
    _textLabel.backgroundColor = RGBA(243, 109, 109, 1);
}

-(void)timerLabel
{
    time += 1;
    _timeLabel.text = [NSString stringWithFormat:@"%ld″",(long)time];
}

-(void)setVoiceImage {

    _recordAnimationView.image = [UIImage imageNamed:@"voicesearchfeedback001″"];
    double voiceSound = 0;
    voiceSound = [[EMCDDeviceManager sharedInstance] emPeekRecorderVoiceMeter];
    if (0 < voiceSound <= 0.05) {
        [_recordAnimationView setImage:[UIImage imageNamed:@"voicesearchfeedback002″"]];
    }else if (0.05<voiceSound<=0.10) {
        [_recordAnimationView setImage:[UIImage imageNamed:@"voicesearchfeedback003″"]];
    }else if (0.10<voiceSound<=0.15) {
        [_recordAnimationView setImage:[UIImage imageNamed:@"voicesearchfeedback004″"]];
    }else if (0.15<voiceSound<=0.20) {
        [_recordAnimationView setImage:[UIImage imageNamed:@"voicesearchfeedback005″"]];
    }else if (0.20<voiceSound<=0.25) {
        [_recordAnimationView setImage:[UIImage imageNamed:@"voicesearchfeedback006″"]];
    }else if (0.25<voiceSound<=0.30) {
        [_recordAnimationView setImage:[UIImage imageNamed:@"voicesearchfeedback007″"]];
    }else if (0.30<voiceSound<=0.35) {
        [_recordAnimationView setImage:[UIImage imageNamed:@"voicesearchfeedback008″"]];
    }else if (0.35<voiceSound<=0.40) {
        [_recordAnimationView setImage:[UIImage imageNamed:@"voicesearchfeedback009″"]];
    }else if (0.40<voiceSound<=0.45) {
        [_recordAnimationView setImage:[UIImage imageNamed:@"voicesearchfeedback010″"]];
    }else if (0.45<voiceSound<=0.50) {
        [_recordAnimationView setImage:[UIImage imageNamed:@"voicesearchfeedback011″"]];
    }else if (0.50<voiceSound<=0.55) {
        [_recordAnimationView setImage:[UIImage imageNamed:@"voicesearchfeedback012″"]];
    }else if (0.55<voiceSound<=0.60) {
        [_recordAnimationView setImage:[UIImage imageNamed:@"voicesearchfeedback013″"]];
    }else if (0.60<voiceSound<=0.65) {
        [_recordAnimationView setImage:[UIImage imageNamed:@"voicesearchfeedback014″"]];
    }else if (0.65<voiceSound<=0.70) {
        [_recordAnimationView setImage:[UIImage imageNamed:@"voicesearchfeedback015″"]];
    }else if (0.70<voiceSound<=0.75) {
        [_recordAnimationView setImage:[UIImage imageNamed:@"voicesearchfeedback016″"]];
    }else if (0.75<voiceSound<=0.80) {
        [_recordAnimationView setImage:[UIImage imageNamed:@"voicesearchfeedback017″"]];
    }else if (0.80<voiceSound<=0.85) {
        [_recordAnimationView setImage:[UIImage imageNamed:@"voicesearchfeedback018″"]];
    }else if (0.85<voiceSound<=0.90) {
        [_recordAnimationView setImage:[UIImage imageNamed:@"voicesearchfeedback019″"]];
    }else if (0.90<voiceSound<=0.95) {
        [_recordAnimationView setImage:[UIImage imageNamed:@"voicesearchfeedback020″"]];
    }else {
        [_recordAnimationView setImage:[UIImage imageNamed:@"voicesearchfeedback020″"]];
    }
}

@end
